﻿$(document).ready(function() {

	var columnsData = [ [ {
		field : 'id',
		title : '编号',
		width : 250,
		sortable : true,
	}, {
		field : 'name',
		title : '中文名',
		width : 200,
		editor : 'text',
	}, {
		field : 'enName',
		title : '英文名',
		width : 200,
		editor : 'text',
	}, {
		field : 'turn',
		title : '兄弟排名',
		width : 100,
		editor : 'text',
	}, {
		field : 'parentId',
		title : '父编号',
		width : 250,
	}, {
		field : 'description',
		title : '描述',
		width : 200,
		editor : 'text',
	}, ] ];

	toolbarData = function(tableId, parentId) {
		var lastIndex;
		var result = [ {
			id : 'btnsearch',
			text : '查看子分类',
			iconCls : 'icon-search',
			handler : function() {
				var selected = $(tableId).datagrid('getSelected');
				if (selected) {
					secondclassifygrid(selected.id);
				} else {
					alert("请选定行！");
				}
			}
		}, '-', {
			id : 'btnpencil',
			text : '修改',
			iconCls : 'icon-edit',
			handler : function() {
				var row = $(tableId).datagrid('getSelected');
				if (!row) {
					alert("请选定行！");
					return;
				}
				var rowIndex = $(tableId).datagrid('getRowIndex', row);
				if (lastIndex != rowIndex || lastIndex == 0) {
					$(tableId).datagrid('endEdit', lastIndex);
					$(tableId).datagrid('beginEdit', rowIndex);
				}
				lastIndex = rowIndex;
			}
		}, '-', {
			id : 'btnappend',
			text : '添加',
			iconCls : 'icon-add',
			handler : function() {
				$(tableId).datagrid('endEdit', lastIndex);
				$(tableId).datagrid('appendRow', {
					id : '',
					parentId : parentId,
				});
				var rowIndex = $(tableId).datagrid('getRows').length - 1;
				$(tableId).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
				lastIndex = rowIndex;
			}
		}, '-', {
			id : 'btnremove',
			text : '删除',
			iconCls : "icon-remove",
			handler : function() {
				var row = $(tableId).datagrid('getSelected');
				if (!row) {
					alert("请选定行！");
					return;
				}
				;

				$('#dialog').dialog({
					title : 'Dialog',
					width : 300,
					height : 150,
					closed : false,
					cache : false,
					modal : true,
					content : '是否删除?',
					buttons : [ {
						text : '确定',
						handler : function() {
							var rowIndex = $(tableId).datagrid('getRowIndex', row);
							$(tableId).datagrid('deleteRow', rowIndex);
							var date = {
								id : row.id
							};
							$.post('/shopping/admin/categoryDelete', date, function(result) {
								$.messager.show({
									title : '提示信息',
									msg : '操作成功！'
								});
							});
							$('#dialog').dialog('close');
						}
					}, {
						text : '取消',
						handler : function() {
							$('#dialog').dialog('close');
						}
					} ]
				});

			}
		}, '-', {
			id : 'btnsave',
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				$(tableId).datagrid('endEdit', lastIndex);
			}
		} ]
		return result;
	}

	secondclassifygrid = function(id) {
		$('#win').window({
			title : "小分类",
			width : 700,
			height : 400,
			modal : true,
			closed : true,
			shadow : true,
		});
		$('#win').window('open');
		$('#categoryChilderList').datagrid({
			width : 600,
			height : 250,
			nowrap : false,
			striped : true,
			fit : true,
			sortName : 'id',
			remoteSort : false,
			pagination : false,
			singleSelect : true,
			fitColumns : true,
			url : '/shopping/admin/categoryChilderList?c1=' + id,
			toolbar : false,
			columns : columnsData,
			toolbar : toolbarData('#categoryChilderList', id),
			onClickRow : function(rowIndex, rowData) {
			},
			onAfterEdit : function(index, record) {
				var id = record.id;
				var name = record.name;
				var turn = record.turn;
				var parentId = record.parentId;
				if (id == "") {
					if (name == "" || turn == "") {
						$('#categoryChilderList').datagrid('deleteRow', index);
						return false;
					}
				}
				$.post('/shopping/admin/categorySaveOrUpdate', record, function(result) {
					$.messager.show({
						title : '提示信息',
						msg : '操作成功！'
					});
				});
			},
		});
	};

	$("#classifyselect").click(function() {
		$('#tt1').datagrid({
			title : '分类信息',
			width : 700,
			height : 250,
			nowrap : false,
			striped : true,
			fit : true,
			sortName : 'id',
			remoteSort : false,
			pagination : false,
			singleSelect : true,
			fitColumns : true,
			url : "/shopping/admin/categoryList",
			toolbar : false,
			columns : columnsData,
			toolbar : toolbarData('#tt1', '0'),
			onAfterEdit : function(index, record) {
				var id = record.id;
				var name = record.name;
				var turn = record.turn;
				var parentId = record.parentId;
				if (id == "") {
					if (name == "" || turn == "") {
						$('#tt1').datagrid('deleteRow', index);
						return false;
					}
				}
				$.post('/shopping/admin/categorySaveOrUpdate', record, function(result) {
					$.messager.show({
						title : '提示信息',
						msg : '操作成功！'
					});
				});
			},
		});
	});

});
